package com.njtoyo.taxi.admin.library.payment.ali;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConstants;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.njtoyo.taxi.admin.library.payment.common.PayBasicInfo;
import lombok.SneakyThrows;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Dell
 * @Date 2021/5/31 15:20
 */
@Component
public class AliPayClient {

    @Autowired
    private AliPayConfig aliPayConfig;

    @SneakyThrows
    public boolean refund(PayBasicInfo order) {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                aliPayConfig.getAppId(), aliPayConfig.getPrivateKey(),
                "json", AlipayConstants.CHARSET_UTF8, aliPayConfig.getPublicKey(), "RSA2");
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("out_trade_no", order.getOutTradeNo());
        jsonObject.put("refund_amount", order.getRefundAmount());
        jsonObject.put("out_request_no", order.getRufundNo());
        request.setBizContent(jsonObject.toString());
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        return response.isSuccess();
    }

}
